﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 动作的Viewer 数据结构体
/// </summary>
public class ActionClipInfo
{
    /// <summary>
    /// 约定的每秒帧数
    /// </summary>
    private const int AnimFrame = 30;
    /// <summary>
    /// 动画名
    /// </summary>
    public string AnimName = string.Empty;
    /// <summary>
    /// 动画总时间
    /// </summary>
    public float AnimTime = 0;
    /// <summary>
    /// 动画总帧数
    /// </summary>
    public int FrameCount = 0;
    /// <summary>
    /// 动画的开始时间段
    /// </summary>
    public float startNormarlizeTime = 0;
    /// <summary>
    /// 一帧时间
    /// </summary>
    private float FrameRatio = 0;
   
    /// <summary>
    /// 所有帧信息
    /// </summary>
    private List<FrameInfo> FrameList = new List<FrameInfo>();

    private Data.Action mAction = null;

    public List<FrameInfo> GetFrameInfo()
    {
        return FrameList;
    }

    private bool isComplateAction = false;

    /// <summary>
    /// 获取Action bondle缩放
    /// </summary>
    /// <returns></returns>
    public Vector3 GetActionBoundleRotate()
    {
        return  new Vector3(mAction.BoundingWidthRadio, mAction.BoundingHeightRadio, mAction.BoundingLengthRadio);
    }
    /// <summary>
    /// 一个Action 有一个潜在的动作信息
    /// 通过这个动作信息进行帧列表构建
    /// </summary>
    /// <param name="clipInfo"></param>
    /// <param name="actionData"></param>
    public ActionClipInfo(AnimClipInfo clipInfo,Data.Action actionData)
    {
        if (clipInfo == null)
        {

            isComplateAction = false;
            return;
        }
        else
        {
            isComplateAction = true;
            //TODO 
            mAction = actionData;
            FrameRatio = 1f / 30f;
            AnimName = clipInfo.AnimName;
            AnimTime = actionData.AnimTime / 1000f; // clipInfo.AnimTime;//动画总时间
            // 时间(s) * 每秒帧数(30) = 该动画时间的总帧数 
            FrameCount = Mathf.CeilToInt(AnimTime * AnimFrame) + 1; //初始状态的时候也是一个帧
            startNormarlizeTime = actionData.AnimSlotList[0].Start / 100f;
            //添加进帧列表
            for (int i = 0; i < FrameCount; i++)
            {
                var curFrameTime = FrameRatio * (i);
                var frameInfo = new FrameInfo(AnimName, i + 1, FrameRatio, curFrameTime, curFrameTime / AnimTime);
                FrameList.Add(frameInfo);
            }

            InitActionFrameData();
        }
    }
    
    /// <summary>
    /// 用Action数据对Viewer 构建帧信息列表
    /// </summary>
    private void InitActionFrameData()
    {
        var intt = mAction.ActionInterrupts;
        var evt = mAction.Events;
        var atkDef = mAction.AttackDefs;
        //构建帧列表中的中断
        for (int i = 0; i < intt.Count; i++)
        {
            SetInterruptsFrame(mAction, intt[i]);
        }
        //构建帧列表中的事件
        for (int i = 0; i < evt.Count; i++)
        {
            SetInterruptsFrame(mAction, evt[i]);
        }
        //构建帧列表中的攻击定义
        for (int i = 0; i < atkDef.Count; i++)
        {
            SetInterruptsFrame(mAction, atkDef[i]);
        }
    }

    private void SetInterruptsFrame(Data.Action action,Data.ActionInterrupt intt)
    {
        //intt.ConnectTime 是Action动画的的Ratio值 ，在这个时间点去触发判定该Intterrupt
        //Action 动画时间， PoseTime 算在动画时间里么 PoseTime 和 BlendTime
        float checkTime = intt.ConnectTime <=100 ? action.AnimTime * intt.ConnectTime / 100 :// [0-100]
            action.AnimTime + action.PoseTime * (intt.ConnectTime - 100) / 100;//[100-200] PoseTime
        for (int i = 0; i < FrameList.Count; i++)
        {
            if (FrameList[i].FrameTime >= checkTime)
            {
                FrameList[i].interrupt = intt;
            }
        }


    }
    private void SetInterruptsFrame(Data.Action action,Data.Event evt)
    {
        float checkTime = evt.TriggerTime <= 100 ? action.AnimTime * evt.TriggerTime / 100 :// [0-100]
           action.AnimTime + action.PoseTime * (evt.TriggerTime - 100) / 100;//[100-200] PoseTime
        for (int i = 0; i < FrameList.Count; i++)
        {
            if (FrameList[i].FrameTime >= checkTime)
            {
                FrameList[i].mEvent  = evt;
            }
        }
    }
    private void SetInterruptsFrame(Data.Action action,Data.AttackDef atk)
    {
        float checkTime = atk.TriggerTime <= 100 ? action.AnimTime * atk.TriggerTime / 100 :// [0-100]
         action.AnimTime + action.PoseTime * (atk.TriggerTime - 100) / 100;//[100-200] PoseTime
        for (int i = 0; i < FrameList.Count; i++)
        {
            if (FrameList[i].FrameTime >= checkTime)
            {
                FrameList[i].attackDef = atk;
            }
        }
    }


    /// <summary>
    /// 将Viewer数据进行存储
    /// </summary>
    public void SaveViewToActionData()
    {

    }
}
